package com.dong.rabbitmq.topic;

import com.dong.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

/**
 * @author dongjian
 * topic主题模式
 * 消费者01接收消息
 */
public class ReceiveMsgTopic01 {

    /**
     * topic交换机名称
     */
    public static final String EXCHANGE_NAME = "topic_exchange";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtil.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        // 声明一个队列
        String queueName = "Q1";
        channel.queueDeclare(queueName, false, false, false, null);
        // 交换机与队列绑定
        channel.queueBind(queueName, EXCHANGE_NAME, "*.orange.*");
        System.out.println("Q1等待接收消息......");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接收消息====>" + new String(message.getBody(), "UTF-8"));
            System.out.println("接收队列==>" + queueName + " 路由键==>" + message.getEnvelope().getRoutingKey());
        };
        // 消费消息
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}
